package com.tanhua.sso.controller;

import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.ResultSet;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    /**
     * 完善个人信息-基本信息
     */
    @PostMapping("loginReginfo")
    public ResponseEntity<Object> saveUserInfo(@RequestBody Map<String,String > param,
                                               @RequestHeader("Authorization") String token){
        try {
            Boolean bool = this.userInfoService.saveUserInfo(param);
            if (bool){//保存中未出现异常，收到Service true的回应
                return ResponseEntity.ok(null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        ErrorResult result = ErrorResult.builder().errCode("000001").errMessage("保存失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
    }

    /**
     * 完善个人信息-上传头像
     * @param file
     * @param token
     * @return
     */
    @PostMapping("loginReginfo/head")
    public ResponseEntity<Object> saveUserLogo(@RequestParam("headPhoto") MultipartFile file,
                                               @RequestHeader("Authorization") String token){
        System.out.println("start:"+file.getOriginalFilename());
        try {
            System.out.println("try:"+file.getOriginalFilename());
            Boolean result= this.userInfoService.saveUserLogo(file,token);
            System.out.println("try-end:"+file.getOriginalFilename()+"result:"+result);
            if (result){
                System.out.println(result);
                return ResponseEntity.ok(null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        ErrorResult result = ErrorResult.builder().errCode("000001").errMessage("保存失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
    }
}
